<?php

declare(strict_types=1);

namespace App\CustHub\Controller\Permission;

use App\Common\Constants\PermissionTypeConstants;
use App\Common\Request\CommonRoleRequest;
use App\CustHub\Annotation\ApiAuth;
use App\CustHub\Annotation\ChOperationLog;
use App\CustHub\Annotation\ChPermission;
use App\CustHub\Annotation\VerifySignature;
use App\CustHub\Controller\BaseController;
use App\CustHub\Request\ChRoleRequest;
use App\CustHub\Service\RoleService;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\PostMapping;
use Mine\Annotation\OperationLog;
use Mine\Annotation\Permission;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
use Psr\Http\Message\ResponseInterface;

/**
 * 员工角色管理
 */
#[Controller(prefix: 'ch/permission/role'),ApiAuth,VerifySignature]
class RoleController extends BaseController
{
    #[Inject]
    protected RoleService $service;

    /**
     * 角色分页列表
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping("index"), ChPermission("ch:permission:role, ch:permission:role:index")]
    public function index(): ResponseInterface
    {
        $data = $this->request->all();
        $data['permission_type'] = PermissionTypeConstants::CUST_HUB;
        $data['select'] = 'id,name';
        $data['pageSize'] = $data['page_size'];
        return $this->success($this->service->getPageList($data,false));
    }

    /**
     * 新增角色
     * @param ChRoleRequest $request
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping("save"), ChOperationLog]
    public function save(ChRoleRequest $request): ResponseInterface
    {
        return $this->success(['id' => $this->service->save($request->all())]);
    }

    /**
     * 更新角色
     * @param ChRoleRequest $request
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping("update"), ChOperationLog]
    public function update(ChRoleRequest $request): ResponseInterface
    {
        return $this->service->update($request->input('id'),$request->all()) ? $this->success() : $this->error();
    }

    #[PostMapping('delete'), ChOperationLog]
    public function delete(ChRoleRequest $request): ResponseInterface
    {
        return $this->service->realDelete([$request->input('id')]) ? $this->success() : $this->error();
    }

    /**
     * 更新用户菜单权限.
     * @param ChRoleRequest $request
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping('menuPermission'), ChOperationLog]
    public function menuPermission(ChRoleRequest $request): ResponseInterface
    {
        $data['menu_ids'] = json_decode($request->input('menu_ids'),true);
        return $this->service->update($request->input('id'), $data) ? $this->success() : $this->error();
    }

    /**
     * 获取角色列表
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface
     */
    #[PostMapping('list')]
    public function list(): ResponseInterface
    {
        $data = $this->request->all();
        $data['permission_type'] = PermissionTypeConstants::CUST_HUB;
        $data['select'] = 'id,name';
        return $this->success($this->service->getList($data,false));
    }

    /**
     * 通过角色获取菜单
     * @param int $id
     * @return ResponseInterface
     * @throws \Psr\Container\ContainerExceptionInterface
     * @throws \Psr\Container\NotFoundExceptionInterface
     */
    #[PostMapping("getMenuByRole")]
    public function getMenuByRole(ChRoleRequest $request): ResponseInterface
    {
        return $this->success($this->service->getMenuByRole((int)$request->input('id')));
    }

}